package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CourseOrder;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CourseOrderQuery;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICourseOrderService;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/courseOrder")
public class CourseOrderController {

    @Autowired
    private ICourseOrderService courseOrderService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ICustomerService customerService;


    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CourseOrderQuery qo) {


        //获得当前课程的id
        Long[] titleIds = courseOrderService.selectTitleIds();
        //Long[] titleIds = {13L, 14L, 15L};
        //查询客程
        List<SystemDictionaryItem> titles = systemDictionaryItemService.selectTitle(titleIds);
        model.addAttribute("titles", titles);

        //查询客户
        List<Customer> customers = customerService.listAll();
        model.addAttribute("customers", customers);


        //调用service的方法的到结果集
        PageInfo pageInfo = courseOrderService.query(qo);
        //共享结果集：在页面遍历显示
        model.addAttribute("result", pageInfo);
        return "courseOrder/list"; //返回视图名称：加上前缀和后缀的ftl
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(CourseOrder courseOrder) {
        if (courseOrder.getId() == null) { // 代表新增
            courseOrderService.save(courseOrder);
        } else { // 代表修改
            courseOrderService.update(courseOrder);
        }
        return new JsonResult();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        //int i = 1/0;
        courseOrderService.delete(id);
        return new JsonResult();
    }
}
